package com.easy.order.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.Order;
import com.easy.common.service.IOrderService;
import com.easy.order.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    // Order相关实现
    @Override
    public Order getOrderById(int id) {
        return super.getOne(new QueryWrapper<Order>().eq("id", id));
    }

    @Override
    public Order getOrderByUserId(int userId) {
        return super.getOne(new QueryWrapper<Order>().eq("user_id", userId));
    }

    @Override
    public int addOrder(Order order) {
        // 确保删除状态默认为0(未删除)
        if (order.getIs_del() == null) {
            order.setIs_del(0);
        }
        // 确保订单状态默认为0(未支付)
        if (order.getStatus() == null){
            order.setStatus(0);
        }
        return super.save(order) ? 1 : 0;
    }

    @Override
    public int updateOrder(Order order) {
        return super.updateById(order) ? 1 : 0;
    }

    @Override
    public int deleteOrder(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public List<Order> getAll(Order order) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<Order> getOrderPage(Order order, IPage<Order> page) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        if (order.getId() != null){
            // 展示该id的订单
            queryWrapper.eq(Order::getId, order.getId());
        }
        queryWrapper.like(order.getGoods_name() != null && !order.getGoods_name().isEmpty(), Order::getGoods_name, order.getGoods_name());
        queryWrapper.like(order.getUser_id() != null , Order::getUser_id, order.getUser_id());
        queryWrapper.like(order.getStatus() != null , Order::getStatus, order.getStatus());
        queryWrapper.eq(Order::getIs_del, 0);
        return super.page(page, queryWrapper);
    }

}
